package com.tueye.service.impl.global;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tueye.annotation.GridFieldConfig;
import com.tueye.base.util.BaseService;
import com.tueye.base.util.Page;
import com.tueye.dao.IGlobalDao;
import com.tueye.entity.Sys_glob_classMap;
import com.tueye.service.exception.TuEyeServiceException;
import com.tueye.service.global.IGlobalGridService;
import com.tueye.utils.DateUtils;

@Scope("prototype")
@Component("globalGridService")
public class GlobalGridServiceImpl extends BaseService implements IGlobalGridService{
	
	@Autowired
	@Qualifier("globalDao")
	private IGlobalDao globalDao;
	
	@Override
	public Page pagedQueryByHQL(String hql, int pageNo, int pageSize,
			Object... values)throws TuEyeServiceException {
		return globalDao.pagedQueryByHQL(hql, pageNo, pageSize, values);
	}
	@Override
	public List<Sys_glob_classMap> getclassMap(String hql)
			throws TuEyeServiceException {
		return this.globalDao.getclassMap(hql);
	}
	
	/*
	    @Override
	    public String getJSonStr(Page page, String classStr) {
		// TODO Auto-generated method stub
		return null;
	}**/
	@Override
	public String getJSonStr(Page page) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String getJSonStr(List list, Map pageInfo, String classStr) {
		// TODO Auto-generated method stub
		 
		List mapList = new ArrayList();  
		try{
			Class cl = Class.forName(classStr);

		    Object obj = cl.newInstance();
			
		 	Field[] fileds = cl.getDeclaredFields();
		// Method[] methods = cl.getDeclaredMethods();
			for (int i = 0; i < list.size(); i++) {
				
					Object object = (Object) list.get(i);
					Map cellMap = new HashMap();
					cellMap.put("numid", i + 1);

					Object[] objs = new Object[fileds.length + 1];
					for (Field field : fileds) {
						if (field.isAnnotationPresent(GridFieldConfig.class)) {
							if (field.getName().equalsIgnoreCase("id")) {
								field.setAccessible(true);
								objs[0] = field.get(object);
								break;
							}
						}
					}
					objs[1] = i + 1;
					int t = 2;

					for (Field field : fileds) {
						if (field.isAnnotationPresent(GridFieldConfig.class)) {
							if (field.getName().equalsIgnoreCase("id")) {
								continue;
							}
							field.setAccessible(true);
							Object ddobj = field.get(object);
							if (ddobj instanceof Date) {
								objs[t++] = DateUtils.getDate2Str(
										"yyyy-MM-dd HH:mm", (Date) ddobj);
								continue;
							}
							objs[t++] = ddobj;
						}
					}

					cellMap.put("cell", objs);
					mapList.add(cellMap);
			}
		

		} catch (Exception ex) {
			ex.printStackTrace();
			
		}
		
		pageInfo.put("rows", mapList);
    	com.tueye.ajax.json.JSONObject object = new com.tueye.ajax.json.JSONObject(pageInfo);
    	return object.toString(); 
	}

}